package net.dgg.dggcloud.service.jrcrm.common.webscoket;

import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.jrcrm.common.webscoket.redis.dao.WSRedisDAO;
import net.dgg.dggcloud.service.jrcrm.common.webscoket.redis.dao.impl.WSRedisDAOImpl;
import net.dgg.dggcloud.service.jrcrm.constant.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 信息发布器
 * 
 * @author tanghom
 * 
 */
public class MsgPublisher {

    private static Logger logger = LoggerFactory.getLogger(MsgPublisher.class);

    /**
     * 消息发布器实例
     */
    private static MsgPublisher instance = new MsgPublisher();

    public static WSRedisDAO wsRedisDAO;

    /**
     * 私有构造器
     */
    private MsgPublisher() {
        wsRedisDAO=new WSRedisDAOImpl();
    }

    /**
     * 获取消息发布器实例
     * 
     * @return 消息发布器实例
     */
    public static MsgPublisher getInstance() {
        return instance;
    }


    /**
     * 给所有人发送信息
     * 
     * @param title
     *            接收人
     * @param msg
     *            发送人
     * @param msg
     *            信息
     */
    public void publish(String title, String msg) {
        HashMap<String, Object> param= new HashMap<>();
    	param.put("title", title);
    	param.put("msg", msg);
        param.put("receiver",0L);
        param.put("sendTime", DateUtil.dateToString(new Date()));
        wsRedisDAO.sendMessage(CommonConstant.PUSH_MESSAGE_CHANNEL,param);
    }

    /**
     * 给某人推送信息
     * @param receiver 接收人ID
     * @param msg 消息内容
     * @param data 消息标题
     */
    public void publish(Long receiver,String msg,Object data) {
        try{
            HashMap<String, Object> param= new HashMap<>();
            param.put("receiver",receiver);
            param.put("msg", StringUtils.delHtmlTag(msg));
            param.put("data", JsonUtil.obj2Json(data));
            param.put("sendTime", DateUtil.dateToString(new Date()));
            wsRedisDAO.sendMessage(CommonConstant.PUSH_MESSAGE_CHANNEL,param);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
